package com.itheima.filter.service.impl;



import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.itheima.exception.BusinessException;
import com.itheima.filter.service.inter.EmpService;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.project.*;
import com.itheima.project.emp.Emp;
import com.itheima.project.emp.EmpExpr;
import com.itheima.project.emp.EmpQueryParam;
import com.itheima.project.log.LoginInfo;
import com.itheima.util.JwtUtil;
import com.itheima.pojo.ProjectUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class EmpServiceIml implements EmpService {
    @Resource
    private EmpMapper empMapper;
    @Resource
    private EmpExprMapper empExprMapper;
    @Resource
    private HttpServletRequest request;


    /* @Override
     public PageBean page(Integer page, Integer pageSize) {
         page = (page -1)*pageSize;
         List<Emp> list = empMapper.list(page, pageSize);
          Long count = empMapper.count();
         return new PageBean(count,list);
     }*/
   @Override
   public PageBean page(EmpQueryParam eqp) {
       //设置分页参数
       PageHelper.startPage(eqp.getPage(),eqp.getPageSize());
       //执行查询
       Page<Emp> empPage = empMapper.list(eqp);

       return new PageBean(empPage.getTotal(),empPage.getResult());
   }

    @Override
    @Transactional

    public void saveEmp(Emp emp) {

       ProjectUtil.checkSpace(emp);
       emp.setCreateTime(LocalDateTime.now());
       emp.setUpdateTime(LocalDateTime.now());

       empMapper.saveEmp(emp);
       Integer empId = emp.getId();

        List<EmpExpr> exprList = emp.getExprList();
        if(!CollectionUtils.isEmpty(exprList)){
            ProjectUtil.checkSpace(exprList);
            exprList.forEach(empExpr -> empExpr.setEmpId(empId));
            empExprMapper.insertBatch(exprList);
        }

    }

    @Transactional
    public void deletById(List<Integer> ids) {
        empMapper.deletById(ids);
        empExprMapper.deletById(ids);
    }
    @Transactional
    @Override
    public void modifyEmp(Emp emp) {
        ProjectUtil.checkSpace(emp);
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateEmp(emp);

        Integer id = emp.getId();
        empExprMapper.deletExpr(id);
        List<EmpExpr> exprList = emp.getExprList();
        if(!CollectionUtils.isEmpty(exprList)){
            ProjectUtil.checkSpace(exprList);
            exprList.forEach(empExpr -> empExpr.setEmpId(id));
            empExprMapper.insertBatch(exprList);
        }

    }

    @Override
    public Emp getInfo(Integer id) {

       Emp emp = empMapper.getInfo(id);

        return emp ;
    }

    @Override
    public List queryAllEmp() {

       List list = empMapper.queryAllEmp();

        return list;
    }

    @Override
    public Emp login(LoginInfo emp) {
     Emp e =  empMapper.getUsernameAndPassword(emp);
        return e;
    }

    @Override
    public void changePassword(Password password) {
       if(!password.getPassword().equals(password.getRepassword())){
           throw new BusinessException("两次密码不一致");
       }
        String token = request.getHeader("token");
        Map<String, Object> empInfo = JwtUtil.ParseTwt(token);
        Integer operateEmpId = (Integer)empInfo.get("id");
        password.setId(operateEmpId);
       empMapper.changePassword(password);



    }
}
